//
//  IntroductionViewController.m
//  TestDemo
//
//  Created by zhangwenqiao on 2018/12/5.
//  Copyright © 2018年 zhangwenqiao. All rights reserved.
//

#import "IntroductionViewController.h"

#define VERSION_INFO_CURRENT @"currentversion"

@interface IntroductionViewController () <UIScrollViewDelegate>
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic, strong) UIButton *pushBtn;
@end

@implementation IntroductionViewController

//- (void)viewDidLoad {
//    [super viewDidLoad];
//    // Do any additional setup after loading the view.
//}

//引导页初始化
- (void)guidePageControllerWithImages:(NSArray *)images {
    self.imageArray = [NSMutableArray arrayWithArray:images];
    UIScrollView *scrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height)];
    scrollV.delegate = self;
    scrollV.pagingEnabled = YES;
    // 隐藏滑动条
    scrollV.showsHorizontalScrollIndicator = NO;
    scrollV.showsVerticalScrollIndicator = NO;
    // 取消反弹
    scrollV.bounces = NO;
    for (NSInteger i = 0; i < images.count; i ++) {
        [scrollV addSubview:({
            self.btnEnter = [UIButton buttonWithType:UIButtonTypeCustom];
            self.btnEnter.frame = CGRectMake(Screen_Width * i, 0, Screen_Width, Screen_Height);
            [self.btnEnter setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];;
            self.btnEnter;
        })];
        
//        [self.btnEnter addSubview:({
//            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//            [btn setTitle:@"跳过" forState:UIControlStateNormal];
//            if (i == images.count - 1) {
//                [btn setTitle:@"点击进入" forState:UIControlStateNormal];
//            }
//            btn.frame = CGRectMake(s_w * i, s_h - 50, 100, 30);
//            btn.center = CGPointMake(s_w / 2, s_h - 60);
//            btn.layer.cornerRadius = 4;
//            btn.clipsToBounds = YES;
//            btn.backgroundColor = [UIColor lightGrayColor];
//            [btn addTarget:self action:@selector(clickEnter) forControlEvents:UIControlEventTouchUpInside];
//            btn;
//        })];
        if (i == images.count - 1) {
            self.pushBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [self.pushBtn setTitle:@"进入App" forState:UIControlStateNormal];
            self.pushBtn.frame = CGRectMake(Screen_Width * i, Screen_Height - 150, 100, 30);
            self.pushBtn.center = CGPointMake(Screen_Width / 2, Screen_Height - 160);
            self.pushBtn.layer.cornerRadius = 4;
            self.pushBtn.clipsToBounds = YES;
            self.pushBtn.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
            if (i == 0) {
                self.pushBtn.hidden = NO;
            }else {
                self.pushBtn.hidden = YES;
            }
            [self.pushBtn addTarget:self action:@selector(clickEnter) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:self.pushBtn];
        }
    }
    scrollV.contentSize = CGSizeMake(Screen_Width * images.count, 0);
    [self.view addSubview:scrollV];
    [self.view bringSubviewToFront:self.pushBtn];
    
    // pageControl
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, Screen_Width / 2, 30)];
    self.pageControl.center = CGPointMake(Screen_Width / 2, Screen_Height - 95);
    [self.view addSubview:self.pageControl];
    self.pageControl.numberOfPages = images.count;
}

//按钮的代理方法
- (void)clickEnter
{
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(clickEnter)]) {
        [self.delegate clickEnter];
    }
}

//是否要进行引导页的显示判断
+ (BOOL)isShow
{
    // 读取版本信息
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *localVersion = [user objectForKey:VERSION_INFO_CURRENT];
    NSString *currentVersion =[[NSBundle mainBundle].infoDictionary objectForKey:@"CFBundleShortVersionString"];
    if (localVersion == nil || ![currentVersion isEqualToString:localVersion]) {
        [IntroductionViewController saveCurrentVersion];
        return YES;
    }else
    {
        return NO;
    }
}

// 保存版本信息
+ (void)saveCurrentVersion
{
    NSString *version =[[NSBundle mainBundle].infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setObject:version forKey:VERSION_INFO_CURRENT];
    [user synchronize];
}

#pragma mark - ScrollerView Delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.pageControl.currentPage = scrollView.contentOffset.x / Screen_Width;
}

//进入按钮的显隐
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSInteger index = scrollView.contentOffset.x / Screen_Width;
    NSLog(@"滑动下标--%ld", index);
    if (index == self.imageArray.count - 1) {
        [UIView animateWithDuration:0.6 animations:^{
            CATransition *tration = [CATransition animation];
            tration.duration = 0.6f;
            tration.type = kCATransitionFade;
            [self.pushBtn.layer addAnimation:tration forKey:nil];
            self.pushBtn.userInteractionEnabled = YES;
            self.pushBtn.hidden = NO;
        }];
    }else {
        [UIView animateWithDuration:0.3 animations:^{
            CATransition *tration = [CATransition animation];
            tration.duration = 0.3f;
            tration.type = kCATransitionFade;
            [self.pushBtn.layer addAnimation:tration forKey:nil];
            self.pushBtn.userInteractionEnabled = NO;
            self.pushBtn.hidden = YES;
        }];
    }
}

@end
